package com.goudan.rabbitmq.controller;

import com.goudan.rabbitmq.constant.QueueDefinition;
import com.goudan.rabbitmq.message.MsgData;
import com.goudan.rabbitmq.model.Order;
import com.goudan.rabbitmq.util.EventDispatcherUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Slf4j
@RestController
public class SendMsgController {

    /**
     * 发送延迟消息，单位：毫秒
     * http://localhost:8080/sendDelayMsg/1000
     */
    @GetMapping("/sendDelayMsg/{delayTime}")
    public void sendDelayMsg(@PathVariable Integer delayTime) {
        log.info("当前时间：{},发送一条时长{}毫秒信息给队列", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")), delayTime);
        Order order = new Order();
        order.setOrderNo("66666666666");
        order.setOrderName("测试订单名称");
        order.setAmount(5);
        order.setPrice(new BigDecimal("99.99"));
        MsgData<Order> msgData = new MsgData<>(order);
        EventDispatcherUtil.eventDispatch(msgData, delayTime);
    }

    /**
     * 发送发布订阅消息
     * http://localhost:8080/sendFanoutMsg/666
     */
    @GetMapping("/sendFanoutMsg/{message}")
    public void sendFanoutMsg(@PathVariable String message) {
        log.info("当前时间：{},发送一条信息给队列QC:{}", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")), message);
        MsgData<String> msgData = new MsgData<>("0001", message, "这是我的测订阅消息！");
        EventDispatcherUtil.eventDispatch(msgData);
    }

    /**
     * 发送直连消息
     * http://localhost:8080/sendDirectMsg/龙珠
     */
    @GetMapping("/sendDirectMsg/{message}")
    public String sendDirectMsg(@PathVariable String message) {
        log.info("当前时间：{},发送一条信息给队列QC:{}", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")), message);
        Order order = new Order();
        order.setOrderNo("C889989877992323");
        order.setOrderName(message);
        order.setAmount(100);
        order.setPrice(new BigDecimal("88.88"));
        MsgData<Order> msgData = new MsgData<>(order);
        EventDispatcherUtil.eventDispatch(msgData, QueueDefinition.LGD_DIRECT_ROUTING_KEY);
        return "发送消息成功，消息：" + message;
    }

}
